﻿using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp;

namespace mozhi.smarterp.BasicData.Warehouses
{
    public class Warehouse : AggregateRootBase
    {
        public string Address {  get; private set; }

        /// <summary>
        /// 启用仓位
        /// </summary>
        public bool ActivateLocation {  get; private set; }

        public List<Location> Locations { get; private set; }

        private Warehouse() { }

        public Warehouse(Guid id, string code, string name, string address, bool activateLocation, bool activated) : base(id, code, name, activated)
        {
            Address = address;
            ActivateLocation = activateLocation;

            Locations = new List<Location>();
        }

        public void Change(string code, string name, string address, bool activateLocation , bool activated)
        {
            base.Change(code , name , activated);

            Address = address;
            ActivateLocation = activateLocation;

            if (!activateLocation)
                Locations = new List<Location>();
        }

        public void ChangeLocations(List<Location> locations)
        {
           
            if (locations == null || locations.Count == 0)
            {
                Locations.Clear();
                return;
            }

            if (!ActivateLocation)
                throw new UserFriendlyException("未开启库位，不允许更改库位信息", "100000");

            locations.ForEach(x =>
            {
                AddOrUpdateLocation(x);
            });

            var itemIds = locations.Select(item => item.Id);

            var deleted = Locations.Where(x => !itemIds.Contains(x.Id)).ToList();

            deleted.ForEach(x => Locations.Remove(x));
        }

        private void AddOrUpdateLocation(Location item)
        {
            if (!ActivateLocation)
                throw new UserFriendlyException("未开启库位，不允许更改库位信息", "100000");

            if (Locations.Any(x => x.Id != item.Id && (x.Code == item.Code || x.Name == item.Name)))
                throw new UserFriendlyException($"库位:{item.Code}-{item.Name}，代码或者名称重复", "100000");

            var match = Locations.FirstOrDefault(x => x.Id == item.Id);

            if (match == null)
                Locations.Add(item);
            else
                match.Change(item.Code, item.Name);
        }
    }

    public class Location : EntityBase
    {
        public Guid WarehouseId { get; private set; }

        public string Code { get; private set; }

        public string Name { get; private set; }

        private Location() { }

        public Location(Guid id , string code, string name, Guid warehouseId)
        {
            Id = id;
            Code = code;
            Name = name;
            WarehouseId = warehouseId;

            Validate();
            
        }

        public void Change(string code , string name)
        {
            Code = code;
            Name = name;

            Validate();
        }

        #region
        private void Validate()
        {
            Check.NotNullOrEmpty(Code, nameof(Code));
            Check.NotNull(Name, nameof(Name));
            Check.NotDefaultOrNull<Guid>(WarehouseId, nameof(WarehouseId));
        }
        #endregion
    }
}
